package com.example.demo.controller;

import com.example.demo.constant.Constants;
import com.example.demo.model.BlogInfo;
import com.example.demo.model.Result;
import com.example.demo.service.BlogInfoService;
import com.example.demo.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/blog")
public class BlogInfoController {
    @Autowired
    private BlogInfoService blogInfoService;

    @RequestMapping("/getList")
    public List<BlogInfo> getList(){
        return blogInfoService.getList();
    }

    @RequestMapping("/getBlogDetail")
    public Result getBlogDetail(Integer blogId, HttpServletRequest request){
        log.info("/blog/getBlogDetail, 获取到参数 blogId: {}", blogId);
        if(blogId == null){
            log.error("/blog/getBlogDetail, 输入参数不合法");
            return Result.fail("输入参数不合法");
        }

        BlogInfo blogInfo = blogInfoService.getBlogDetail(blogId);
        if(blogInfo == null){
            log.error("/blog/getBlogDetail, 未查询到博客信息");
            return Result.fail("未查询到博客信息");
        }

        String token = request.getHeader(Constants.USER_INFO_TOKEN);
        Claims claims = JwtUtils.parseToken(token);
        if(blogInfo.getUserId() == claims.get(Constants.USER_ID)){
            blogInfo.setLoginUser(true);
        }
        return Result.success(blogInfo);
    }

    @RequestMapping("/add")
    public Result addBlog(BlogInfo blogInfo, HttpServletRequest request){
        log.info("/add, 接收到参数 blogInfo: {}", blogInfo);
        if(blogInfo == null){
            return Result.fail("输入参数错误");
        }

        String token = request.getHeader(Constants.USER_INFO_TOKEN);
        Claims claims = JwtUtils.parseToken(token);
        blogInfo.setUserId((Integer) claims.get(Constants.USER_ID));

        Integer result = blogInfoService.addBlog(blogInfo);
        if(result != 1){
            log.error("/add, 插入博客失败");
            return Result.fail("插入博客失败");
        }
        return Result.success(result);
    }

    @RequestMapping("/update")
    public Result updateBlog(BlogInfo blogInfo){
        log.info("/blog/update, 接收到参数 blogInfo: {}", blogInfo);
        if(blogInfo == null){
            log.error("/blog/update, 参数不合法");
            Result.fail("参数不合法");
        }
        Integer result = blogInfoService.updateBlog(blogInfo);
        if(result != 1){
            log.error("/blog/update, 更新博客失败");
            return Result.fail("更新博客失败");
        }
        return Result.success(result);
    }

    @RequestMapping("/delete")
    public Result deleteBlog(Integer blogId){
        log.info("/blog/delete, 接收到参数 blogId: {}", blogId);
        if(blogId == null){
            log.error("/blog/update, 参数不合法");
            Result.fail("参数不合法");
        }
        Integer result = blogInfoService.deleteBlog(blogId);
        if(result != 1){
            log.error("/blog/update, 删除博客失败");
            return Result.fail("删除博客失败");
        }
        return Result.success(result);
    }
}